home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / newsgroups / misc.19981211-19990422 / 000395_news@watsun.cc.columbia.edu _Mon Mar 22 02:45:55 1999.msg < prev    next >
Internet Message Format  |  2020-01-01  |  4KB

  1. Return-Path: <news@watsun.cc.columbia.edu>
  2. Received: from newsmaster.cc.columbia.edu (newsmaster.cc.columbia.edu [128.59.59.30])
  3.     by watsun.cc.columbia.edu (8.8.5/8.8.5) with ESMTP id CAA22126
  4.     for <kermit.misc@watsun.cc.columbia.edu>; Mon, 22 Mar 1999 02:45:55 -0500 (EST)
  5. Received: (from news@localhost)
  6.     by newsmaster.cc.columbia.edu (8.8.5/8.8.5) id CAA16220
  7.     for kermit.misc@watsun.cc.columbia.edu; Mon, 22 Mar 1999 02:20:17 -0500 (EST)
  8. X-Authentication-Warning: newsmaster.cc.columbia.edu: news set sender to <news> using -f
  9. From: Mark Sapiro <msapiro@value.net>
  10. Subject: Re: Associative Array in Kermit 95
  11. Date: Sun, 21 Mar 1999 23:10:29 -0800
  12. Organization: Not Very Much
  13. Message-ID: <36F5ECE5.5E8A07D1@value.net>
  14. To: kermit.misc@watsun.cc.columbia.edu
  15.  
  16. Frank da Cruz wrote:
  17. >> 
  18. In article <7cttsc$47f$1@nnrp1.dejanews.com>,  <dn5006@my-dejanews.com> wrote:
  19.  
  20. : Associative array is a very useful feature in scripting language, it enables
  21. : Perl, Tcl etc. the implementation of comlex data structures.  The following
  22. : script demonstrates that associative array can also be crafted in Kermit
  23. : 95. The script counts the unique words in a regular english, french, german,
  24. : etc. text file.
  25. :
  26. : open read testfile.txt
  27. : if fail end 1 Can't open testfile.txt
  28. : assign \%n 0                                  ; init register
  29. : while true {
  30. :     read \%l                                  ; read each line
  31. :     if fail break                             ; until the end of file
  32. :     while > \flength(\%l) 0 {
  33. :         assign \%w \fbreak(\%l,{ })           ; split on space
  34. :         xif defined \m(\%w) {                 ; word already seen?
  35. :             _assign \%w \feval(\m(\%w) + 1)   ; incr count this word
  36. :         } else {
  37. :             _assign \%w 1                     ; init count this word
  38. :             increment \%n                     ; next register
  39. :             _assign \%n \%w                   ; register this word
  40. :         }
  41. :         assign \%l \fltrim(\fright(\%l,-
  42. :         \feval(\flength(\%l)-\flength(\%w)))) ; shift to next word
  43. :     }
  44. : }
  45. : for \%k 1 \%n 1 {
  46. :       assign \%w \m(\%k)                      ; get word from register
  47. :       echo <\m(\%w)> \%w                      ; display occurences
  48. : }
  49. :
  50. : This approach avoids the use of array which has to be declared in advance.
  51. : The script does not take into account the non alphanumeric characters.
  52. :
  53. : Dat Nguyen
  54. : Airline Telecommunications and Information Services
  55. : 770 Sherbrooke West
  56. : Montreal, Quebec
  57. : Canada H3A 1G1
  58. : Email dat.nguyen&sita.int
  59. :
  60. Excellent!  I've had associative arrays on my list for quite a while, but
  61. the list so long and time so short.  I've reformatted your script to fit
  62. in 80 columns.
  63.  
  64. We plan to add a script library to the Kermit website -- this one will
  65. certainly go into it.  Other submissions are welcome too; send them in!
  66. (Be sure to document the Kermit program and version and other relevant
  67. info.)
  68.  
  69. A quick glance shows this script doesn't use any new (post-C-Kermit-6.0)
  70. features, some of which would make it simpler and faster, for example
  71. the new \fword() and \fsplit() functions for extracting words from strings,
  72. with specified break masks (e.g. to make sure punctuation does not count
  73. as part of word (e.g. "thing" and "thing.").  Also you can convert each
  74. word to lowercase with \flower() so "Thing", "thing", and "THING" count as
  75. the same word, etc.
  76.  
  77. Readers should take special note of the "_assign" verb, which is subtly
  78. different from "assign" (see p.457 of the manual).
  79.  
  80. - Frank
  81. >>
  82.  
  83. There is a problem with the above approach to implementing associative
  84. arrays.  Namely, if one of the 'words' used as an index happens to be
  85. equal to or an initial substring of a pre-defined macro (e.g. "cautious" or
  86. "robust") or one of the macros that gets defined implicitly when certain
  87. programming constructs are used (e.g. "break " or "continue"), the
  88. "xif defined" test gives the "wrong" result and the word doesn't get
  89. counted (although the macro 'word' does get (re-)defined, most likely
  90. with an error message from \feval()).
  91.  
  92. This problem could probably be avoided by prepending some prefix to
  93. each word before using it as a macro name.
  94.  
  95. -- 
  96. Mark Sapiro <msapiro@value.net>       The highway is for gamblers,
  97. San Francisco Bay Area, California    better use your sense - B. Dylan